package com.tarena.lbs.user.service.impl;


import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.common.utils.PageResultBuilder;
import com.tarena.lbs.user.api.UserApi;
import com.tarena.lbs.user.api.pojo.dto.AdminDto;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.param.AdminParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.service.AdminService;
import com.tarena.lbs.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
@Slf4j
@RequiredArgsConstructor
public class AdminServiceImpl implements AdminService {

    private final AdminRepository adminRepository;


    @Override
    public AdminVO detail() throws BusinessException {
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        log.info("userPrinciple:{}", userPrinciple);
        //断言异常
        Asserts.isTrue(userPrinciple == null, new BusinessException("-2", "认证解析错误"));
        Integer id = userPrinciple.getId();
        //2.读取数据库po
        AdminPO po = adminRepository.getAdminById(id);
        //3.转化成vo VO就是PO的复制改名 属性几乎一模一样的
        AdminVO vo = null;
        if (po != null) {
            vo = new AdminVO();
            //密码最好设置成空
            BeanUtils.copyProperties(po, vo);
        }
        return vo;
    }

    @Override
    public AdminDto detail(Integer id){
        AdminPO adminPO = adminRepository.getAdminById(id);
        AdminDto dto = null;
        if (adminPO != null){
            dto = new AdminDto();
            BeanUtils.copyProperties(adminPO,dto);
        }
        return dto;
    }
    @Override
    public PageResult<AdminVO> listAdmins(AdminQuery query){
        return PageResultBuilder.build(adminRepository.listAdmins(query));
    }
    @Override
    public void saveAdmin(AdminParam param) throws BusinessException{
        UserPrinciple user = AuthenticationContextUtils.get();
        Asserts.isTrue(user == null, new BusinessException("-2", "认证解析错误"));
        AdminDto thisAdmin = detail(user.getId());
        if (thisAdmin == null){
            throw new BusinessException("-2", "用户为非管理员用户");
        }
        List<AdminVO> allAdmins = adminRepository.listAdmins(new AdminQuery());
        AdminPO po = new AdminPO();
        AdminPO preAdmin = adminRepository.getAdminByPhone(param.getAccountPhone());
        if (preAdmin == null) {
            Asserts.isTrue(
                    allAdmins.stream().anyMatch(
                            adminVO -> adminVO.getAccountPhone().equals(param.getAccountPhone())),
                    new BusinessException("-2", "手机号已存在")
            );
            Asserts.isTrue(
                    allAdmins.stream().anyMatch(
                            adminVO -> adminVO.getBusinessId().equals(param.getBusinessId())),
                    new BusinessException("-2", "商家已存在")
            );
            BeanUtils.copyProperties(param, po);
            po.setCreateTime(new Date());
        }else {
            BeanUtils.copyProperties(param, po);
            po.setCreateTime(preAdmin.getCreateTime());
            adminRepository.delete(preAdmin.getId());
        }
        adminRepository.save(po);
    }

}
